#!/bin/bash

buff_xsel=$(wl-paste | tr -d '\n')

case $buff_xsel in
	http*)
		notify-send " mpv" "Loading video..." && \
			foot -e mpv "$buff_xsel" || \
			notify-send -u critical -t 5000 "mpv" "Bad link for video!"
		;;
	magnet:?xt*)
		# Clean magnet link
		magnet_link=$(echo "$buff_xsel" | sed -n 's/^\(magnet:?xt.\{50\}\).*/\1/p')
		notify-send " webtorrent" "Loadind stream..."
		# Select specific file in torrent
		webtorrent "$buff_xsel" -s -o /tmp/webtorrent > /tmp/webtorrent/list.log
		magnet_num=$(cat /tmp/webtorrent/list.log | \
			sed -n '/^[0-9]\+/p' | \
			sort -k2 -b | \
			fuzzel -d -p "Play torrent file:" -l 20 | \
			sed -n 's/^\([0-9]\+\).*/\1/p')
		[ -n "$magnet_num" ] && \
		foot -e webtorrent "$buff_xsel" -s="$magnet_num" --mpv
		;;
	*)
		trans_lang=$(printf en:ru"\n"ru:en | fuzzel -d -l 2 -p " Translate:")
		[ -n "$trans_lang" ] && \
			# st -e trans -b "$trans_lang" "$buff_xsel"
		trans -v -show-original y \
			-show-translation y \
			-show-prompt-message n \
			-show-translation-phonetics n \
			-show-original-phonetics n \
			-show-original-dictionary n \
			-show-alternatives n \
			-show-dictionary n \
			-show-languages n \
			"$trans_lang" "$buff_xsel" \
			> /tmp/translate.tmp && \
		foot -e vim /tmp/translate.tmp
	;;
esac
